package minirestwebservice;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;

import org.olap4j.OlapException;

@Path("/slicer")
public class SlicerService {

	@GET
	@Produces("text/html")
	public String halloHtml(@QueryParam("dsUri") String dsUri, @QueryParam("ordinal") String ordinal)
			throws OlapException {

		try {
			dsUri = URLDecoder.decode(dsUri, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// ByteArrayOutputStream boas = new ByteArrayOutputStream();
		// //ByteArrayInputStream bais = new
		// ByteArrayInputStream(boas.toByteArray());
		//
		// System.setOut(new PrintStream(boas));

		Slicer slicer = new Slicer(dsUri);

		/*
		 * Coordinates
		 */
//		List<Integer> coordinates = new ArrayList<Integer>();
//
//		// We query for the very first slice
//		coordinates.add(0, 0);
//		coordinates.add(1, 0);
//		coordinates.add(2, 0);
//		coordinates.add(3, 0);
		
		List<Integer> coordinates = slicer.ordinalToCoordinates(new Integer(ordinal));

		// return "Sliced " + dsUri + boas.toString();

		/*
		 * We want to create HTML output of: 1) Dataset 2) Logical Query Plan 3)
		 * SPARQL Query 4) Result
		 */
		String result = "<html><title>Slicer</title><body>";

		result += "<h2>Slicer</h2>";
		result += "<p>Dataset: " + dsUri + "</p>";

		result += "<h3>Logical Query Plan</h3>";
		String logicalqueryplan = slicer.getLogicalQuery(coordinates)
				.toString();
		result += "<p>" + logicalqueryplan + "</p>";

		result += "<h3>Physical Query Plan</h3>";
		String physicalqueryplan = slicer.getPhysicalQuery(coordinates)
				.toString();
		result += "<p>" + physicalqueryplan + "</p>";

		result += "<h3>Result</h3>";
		String results = slicer.getResult(coordinates);
		result += "<p>" + results + "</p>";

		result += "</body></html>";

		return result;
	}
}